package com.burgeon.backend.SHOPBACKEND.rest;
/**
 * 零售单明细
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.OrderDetails;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class QueryOrderDetails extends RESTBase {
    private String ip;
    private String account;
    private String password;

    public QueryOrderDetails(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 接收REP返回的字符串并进行数据处理
     *
     * @return
     */
    public ArrayList<OrderDetails> getData(String id) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(id), url);
        System.err.println(str);
        JSONObject jsonObject = new JSONArray(str).getJSONObject(0);
        String code = jsonObject.getString("code");
        String message = jsonObject.getString("message");
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<OrderDetails> orderDetailsArrayList = new ArrayList<OrderDetails>();
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONArray array = jsonArray.getJSONArray(i);

            OrderDetails orderDetails = new OrderDetails();
            orderDetails.setId(jsonArray.getJSONArray(i).getString(0));
            orderDetails.setYcRetailId(jsonArray.getJSONArray(i).getString(1));
            orderDetails.setcVipId(jsonArray.getJSONArray(i).getString(2));
            orderDetails.setIsDis(jsonArray.getJSONArray(i).getString(3));
            orderDetails.setIdRet(jsonArray.getJSONArray(i).getString(4));
            orderDetails.setmProductaliasId(jsonArray.getJSONArray(i).getString(5));
            orderDetails.setmProductId(jsonArray.getJSONArray(i).getString(6));
            orderDetails.setPricelist(jsonArray.getJSONArray(i).getString(7));
            orderDetails.setPriceactual(jsonArray.getJSONArray(i).getString(8));
            orderDetails.setDiscount(jsonArray.getJSONArray(i).getString(9));
            orderDetails.setQty(jsonArray.getJSONArray(i).getString(10));
            orderDetails.setTotAmtList(jsonArray.getJSONArray(i).getString(11));
            orderDetails.setTotAmtActual(jsonArray.getJSONArray(i).getString(12));
            orderDetails.setType(jsonArray.getJSONArray(i).getString(13));
            orderDetails.setcStoreId(jsonArray.getJSONArray(i).getString(14));
            orderDetails.setSalesrrpId(jsonArray.getJSONArray(i).getString(15));
            orderDetails.setmRetailId(jsonArray.getJSONArray(i).getString(16));
            orderDetails.setmRetailitemId(jsonArray.getJSONArray(i).getString(17));
            orderDetails.setUniqueid(jsonArray.getJSONArray(i).getString(18));
            orderDetails.setStatus(jsonArray.getJSONArray(i).getString(19));
            orderDetails.setOrderno(jsonArray.getJSONArray(i).getString(20));

            orderDetailsArrayList.add(orderDetails);
        }

        return orderDetailsArrayList;
    }

    /**
     * 准备向ERP传送字段拼接
     *
     * @return
     */
    private MultiValueMap Data(String id) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段
        columns.add("ID");//ID
        columns.add("YC_RETAIL_ID");// 云订单
        columns.add("C_VIP_ID");// 购买顾客
        columns.add("IS_DIS");//是否发货
        columns.add("IS_RET");//是否退货
        columns.add("M_PRODUCTALIAS_ID");//条码
        columns.add("M_PRODUCT_ID");// 商品
        columns.add("PRICELIST");//零售价
        columns.add("PRICEACTUAL");//成交价
        columns.add("DISCOUNT");//折扣
        columns.add("QTY");// 	数量
        columns.add("TOT_AMT_LIST");// 零售金额
        columns.add("TOT_AMT_ACTUAL");//成交金额
        columns.add("TYPE");//零售类型
        columns.add("C_STORE_ID ");//发货店仓
        columns.add("SALESREP_ID");//营业员
        columns.add("M_RETAIL_ID");//生成零售单号
        columns.add("M_RETAILITEM_ID");//对应零售单明细ID
        columns.add("DESCRIPTION");//备注
        columns.add("UNIQUEID");//线上唯一标识
        columns.add("STATUS");//状态
        columns.add("ORDERNO");//序号

        params.put("columns", columns);
        params.put("table", "YC_RETAILITEM");//表名

        //设置排序规则
        HashMap orderby = new HashMap();
        orderby.put("column", "ID");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        HashMap paramss = new HashMap();
        paramss.put("column", "YC_RETAIL_ID");
        paramss.put("condition", id);

        params.put("params", paramss);

        return DATA("Query", params, account, password);

    }

    /**
     * 执行方法，向ERP传参
     *
     * @param args
     */
    public static void main(String[] args) {
        QueryOrderDetails queryOrderDetails = new QueryOrderDetails("39.100.45.8:55322", "nea@burgeon.com.cn", "123");

        try {
            System.err.println(queryOrderDetails.getData("2"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}